﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Entities;
using ARR.DataModel.Repositories;
using DDF.Entity;
using System.Transactions;
using ARR.Shared;
using ARR.Services.BizRules;

namespace ARR.Services.Domain
{
    public static class StoresDomain
    {
        public static List<Store> GetAll(bool lazyLoad)
        {
            var rows = StoresRepository.Select();
            var stores = rows.ConvertAll<Store>(c => new Store(c));

            if (!lazyLoad)
            {
                var countries = CountriesDomain.GetAll();
                foreach (var store in stores)
                {
                    store.Country = countries.Where(x => x.Id == store.CountryId).FirstOrDefault();
                }
            }

            stores.ForEach(st => st.ChangeState(EntityState.Original));

            return stores;
        }

        public static List<Store> GetByCountry(int countryId, bool lazyLoad)
        {
            var rows = StoresRepository.Select(x => x.CountryId == countryId);
            var stores = rows.ConvertAll<Store>(c => new Store(c));

            if (!lazyLoad)
            {
                var countries = CountriesDomain.GetAll();
                foreach (var store in stores)
                {
                    store.Country = countries.Where(x => x.Id == store.CountryId).FirstOrDefault();
                }
            }

            stores.ForEach(st => st.ChangeState(EntityState.Original));

            return stores;
        }

        public static Store GetById(int storeId)
        {
            var row = StoresRepository.Select(st => st.Id == storeId).SingleOrDefault();
            var store = new Store(row);

            store.ChangeState(EntityState.Original);

            return store;
        }

        public static void DeleteStores(List<Store> stores)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    foreach (Store store in stores)
                    {
                        Delete(store);
                    }

                    scope.Complete();
                }
            }
            catch
            {
                throw;
            }
        }

        public static void Delete(Store store)
        {
            try
            {
                StoreBizRules.ValidateDelete(store);

                StoresRepository.Delete(st => st.Id == store.Id);
            }
            catch
            {
                throw;
            }
        }

        public static void SaveStores(List<Store> stores)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                foreach (Store store in stores)
                {
                    Save(store);
                }

                scope.Complete();
            }
        }

        public static void Save(Store store)
        {
            StoreBizRules.ValidateSave(store);

            if (store.RowState == EntityState.New)
            {
                StoresRepository.Insert(store.GetRow());
            }
            else if (store.RowState == EntityState.Modified)
            {
                StoresRepository.Update(store.GetRow());
            }
        }

        public static void SaveTempStores(List<TempStore> stores)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                foreach (TempStore store in stores)
                {
                    //TODO: Ver como implementar validaciones para los Temp, para poder usar el mismo codigo.
                    //StoreBizRules.ValidateSave(store);

                    if (store.GetEntityState() == EntityState.New)
                    {
                        TempStoresRepository.Insert(store.GetRow());
                    }
                    else if (store.GetEntityState() == EntityState.Modified)
                    {
                        TempStoresRepository.Update(store.GetRow());
                    }
                }

                scope.Complete();
            }
        }
    }
}
